package net.caixiaomi.info.ui.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.MatchFilterAdapter;
import net.caixiaomi.info.base.BaseDialogFragment;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.model.MatchItem;
import net.caixiaomi.info.util.EventWhat;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static android.R.id.button1;
import static android.R.id.button2;
import static android.R.id.list;

/**
 * 选择串关
 */

public class FootballSeriesDialogFragment extends BaseDialogFragment implements BaseQuickAdapter.OnItemClickListener {

    @BindView(list)
    RecyclerView mListView;
    private MatchFilterAdapter mAdapter;

    private List<MatchItem> mData = new ArrayList<>();

    @OnClick(button1)
    void toConfirm() {
        try {
            String strData = JSON.toJSONString(mData);
            if (!TextUtils.isEmpty(strData)) {
                EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_SERIES, strData));
            }
            dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick(button2)
    void toCancel() {
        dismiss();
    }

    @Override
    protected int layoutRes() {
        return R.layout.dialog_football_series;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String strData = getArguments().getString("data");
        List<MatchItem> data = JSON.parseObject(strData, new TypeToken<List<MatchItem>>() {
        }.getType());
        if (data != null && !data.isEmpty()) {
            mData.addAll(data);
        }
    }

    @Override
    protected void onViewCreated(@NonNull View view) {
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wlp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        wlp.gravity = Gravity.BOTTOM;
        window.setAttributes(wlp);
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        window.setBackgroundDrawable(new ColorDrawable(0xff000000));
        getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
        mListView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        mListView.setNestedScrollingEnabled(false);
        mAdapter = new MatchFilterAdapter(R.layout.view_textview);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setNewData(mData);
        mListView.setAdapter(mAdapter);

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        try {
            if (mAdapter.getData().size() == 1)
                return;
            MatchItem matchItem = mAdapter.getData().get(position);
            matchItem.setSelect(!matchItem.isSelect());
            mAdapter.notifyItemChanged(position);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
